package com.wbl.tijiancms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wbl.tijiancms.pojo.Users;
import com.wbl.tijiancms.response.ApiRestResponse;
import com.wbl.tijiancms.response.TijianExceptionEnum;
import com.wbl.tijiancms.service.UsersService;
import com.wbl.tijiancms.mapper.UsersMapper;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.util.List;


/**
* @author yy
* @description 针对表【users】的数据库操作Service实现
* @createDate 2024-09-18 10:09:07
*/
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users>
    implements UsersService{

    @Autowired
    UsersMapper usersMapper;

    @Resource
    StringEncryptor encryptor;

    @Override
    public ApiRestResponse login(Users users){
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",users.getUserid());
        Users loginUser = usersMapper.selectOne(queryWrapper);
        if (loginUser == null) {
            // 用户不存在
            return ApiRestResponse.error(1005,"用户不存在");
        }
        loginUser.setPassword(encryptor.decrypt(loginUser.getPassword()));
        loginUser.setIdentitycard(encryptor.decrypt(loginUser.getIdentitycard()));
        if(loginUser.getPassword().equals(users.getPassword())) {
            System.out.println(loginUser);
            return ApiRestResponse.success(loginUser);
        }else {
            return ApiRestResponse.error(1005,"密码错误");
        }
    }

    @Override
    public ApiRestResponse register(Users users) {
        Users u = usersMapper.selectByUserid(users.getUserid());
        if(u==null){
            users.setPassword(encryptor.encrypt(users.getPassword()));
            users.setIdentitycard(encryptor.encrypt(users.getIdentitycard()));
            System.out.println(users.getPassword());
            users.setUsertype(1);
            int i = usersMapper.insert(users);
            if(i>0) {
                return ApiRestResponse.success(u);
            }else {
                return ApiRestResponse.error(1006,"注册失败，刷新后重试");
            }
        }
        return ApiRestResponse.error(1006,"注册失败，刷新后重试");
    }

    @Override
    public ApiRestResponse getUserAll() {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("usertype",1);
        List<Users> users = usersMapper.selectList(queryWrapper);
        if(users.isEmpty()){
            return ApiRestResponse.error(1006,"获取失败");
        }else {
            for(Users user : users){
                user.setPassword(encryptor.decrypt(user.getPassword()));
                user.setIdentitycard(encryptor.decrypt(user.getIdentitycard()));
            }
            return ApiRestResponse.success(users);
        }
    }


    @Override
    public ApiRestResponse getUserByuserid(String userid) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userid);
        Users user = usersMapper.selectOne(queryWrapper);
        if(user==null){
            return ApiRestResponse.error(TijianExceptionEnum.SYSTEM_ERROR);
        }
        user.setPassword(encryptor.decrypt(user.getPassword()));
        user.setIdentitycard(encryptor.decrypt(user.getIdentitycard()));
        return ApiRestResponse.success(user);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiRestResponse editPassword(Users user) throws Exception {
        UpdateWrapper<Users> updateWrapper = new UpdateWrapper<>();
        user.setPassword(encryptor.encrypt(user.getPassword()));
        updateWrapper.eq("userid",user.getUserid()).set("password",user.getPassword());
        usersMapper.update(null,updateWrapper);
        if(user==null){
            return ApiRestResponse.error(TijianExceptionEnum.SYSTEM_ERROR);
        }
        return ApiRestResponse.success();
    }
}




